<?php

/*
 * Created by: Dikkehein <Clemens Wijnekus, c.wijnekus@gmail.com>
 */


class Home extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->model('Kampjaar_model');
        $this->load->model('Schreeuw_model');
        $this->load->model('Ap_model');
        
    }
    
    public function index()
    {
        if ($this->session->userdata('logged_in') == TRUE) {
            
            $kampjaarResult = $this->Kampjaar_model->huidig_kampjaar();
            $data['gebruiker'] = $this->session->all_userdata();
            $data['schreeuwen'] = $this->Schreeuw_model->alle_schreeuwen_voor_kampjaar($data['gebruiker']['kampjaar']);
            $data['title'] = 'Home';
            $data['actiepunten'] = $this->Ap_model->alle_actiepunten_voor_kampjaar($data['gebruiker']['kampjaar']);

            // Load support assets
            $this->form_validation->set_error_delimiters('', '<br />');

            // Set validation rules
            $this->form_validation->set_rules('bericht', 'Bericht', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('id_gebruiker', 'ID', 'required|min_length[1]|max_length[1]');

            // Begin validation
            if ($this->form_validation->run() == FALSE) {
                // First load, or problem with form
                $data['bericht'] = array(
                    'name' => 'bericht',
                    'id' => 'bericht',
                    'value' => set_value('bericht', ''),
                    'maxlength' => '125',
                    );

                $data['id'] = array(
                    'id_gebruiker' => set_value('id_gebruiker', $data['gebruiker']['id_gebruiker']));

                
                $this->load->view('partial/header', $data);
                $this->load->view('partial/menu', $data);
                $this->load->view('partial/sidebar', $data);
                $this->load->view('home/index', $data);
                
            } else { // Validation passed, now escape the data
                $data = array(
                    'bericht' => $this->input->post('bericht'),
                    'id_gebruiker' => $this->input->post('id_gebruiker'),
                    'kampjaar' => $kampjaarResult['jaar']
                );
                if ($this->Schreeuw_model->schreeuwen($data)) {
                    redirect('home');
                }
            }
            
            
            
            
        } else {
            redirect('auth');
        }
    }
    
}
